$(function(){
    obj.init();
});

var obj = new JsFun();
function JsFun(){
    'use strict';
    return {
        dataGrid: null,
        init: function () {
            this.dataGrid = new xhDataGrid($('#list'), 'Server/getBox');
            this.dataGrid.parseToolbar().init().activeButton([], ['bt_list_state']);
            this.funObj().bindGridEvent();
            this.listener();
        },
        listener: function () {
            //推送资源
            $('#bt_list_push').click(function(){
                obj.funObj().pushClick();
            });
            //查看状态
            $('#bt_list_state').click(function(){
                if (xhExtra.buttonDisabled($(this))) return false;
                obj.funObj().stateClick();
            });
            //更新版本
            $('#bt_list_update').click(function(){
               obj.funObj().updateClick();
            });
        },
        funObj: function(){
            return {
                bindGridEvent: function() {
                    obj.dataGrid.onLoadSuccess = function(data){
                        obj.dataGrid.clear();
                        if (data.rows.length <= 0) return false;
                        obj.dataGrid.mergeCells(['schoolId', 'schoolName']);
                        //获取盒子网络状态
                        obj.funObj().updatePortNet();
                    };
                },
                pushClick: function() {
                    window.top.swal({
                        title: '推送资源',
                        showCancelButton: true,
                        cancelButtonText: '取消',
                        confirmButtonText: '推送',
                        showLoaderOnConfirm: true,
                        html: '<input type="url" id="push_url" class="swal2-input" placeholder="url地址" />' +
                        '<input id="push_md5" class="swal2-input" placeholder="md5码" />' +
                        '<input type="number" id="push_valid" min="1" class="swal2-input" placeholder="有效期(天)" style="max-width: 100%;"/>',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var params = {
                                    url: [$('#push_url', window.top.document).val()],
                                    md5: [$('#push_md5', window.top.document).val()],
                                    day: $('#push_valid', window.top.document).val()
                                };
                                if (!params.url[0] || !params.md5[0]){
                                    reject('参数不能为空！');
                                    return false;
                                }
                                xhExtra.ajaxRequest('Server/pushBoxResource/3', params, {success: true});
                            })
                        }
                    }).then(function (isConfirm) {}, function (dismiss) {});
                },
                stateClick: function() {
                    window.top.xhTop.showModal({
                        title: '盒子状态',
                        content: xhTool.getFrameString('Server/boxState', {mac: obj.dataGrid.getSelectValue('mac')}, 600),
                        size: 'lg'
                    });
                },
                updateClick: function () {
                    window.top.swal.setDefaults({
                        showCancelButton: true,
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        progressSteps: ['1', '2']
                    });
                    var version;
                    var steps = [
                        {
                            title: '输入版本号',
                            input: 'text',
                            confirmButtonText: '下一步',
                            preConfirm: function(ver) {
                                return new Promise(function (resolve, reject) {
                                    if (!ver) {
                                        reject('版本号不能为空！');
                                    } else {
                                        version = ver;
                                        resolve();
                                    }
                                })
                            }
                        },
                        {
                            title: '选择更新文件',
                            input: 'file',
                            inputAttributes: {accept: '.jar'},
                            confirmButtonText: '更新',
                            preConfirm: function(file) {
                                return new Promise(function (resolve, reject) {
                                    if (!file) {
                                        reject('文件不能为空！');
                                    } else {
                                        var fileData = new FormData();
                                        fileData.append('version', version);
                                        fileData.append('file', file);
                                        $.ajax({
                                            url: baseUrl + 'Server/updateBoxVersion',
                                            type: 'POST',
                                            processData: false,
                                            contentType: false,
                                            data: fileData,
                                            complete: function(XMLHttpRequest){
                                                window.top.swal.resetDefaults();
                                                var result = XMLHttpRequest.responseJSON;
                                                if(xhExtra.checkError(result)) return false;
                                                window.top.xhTop.showMessage({type: 'success', title: result.msg});
                                            }
                                        });
                                    }
                                })
                            }
                        }
                    ];

                    window.top.swal.queue(steps).then(function(result) {}, function(dismiss) {window.top.swal.resetDefaults();});
                },
                updatePortNet: function () {
                    xhExtra.ajaxRequest('Server/getBoxNet', {}, {}, function (result) {
                        console.log(result);
                        var rows = obj.dataGrid.list.datagrid('getRows');
                        $.each(rows, function (index, row) {
                            if (result[row.portnumber]) {
                                obj.dataGrid.list.datagrid('updateRow', {index: index, row: {netStatus: '<i class="fa fa-circle fa-lg text-' + result[row.portnumber] + '"></i>'}});
                            }
                            else {
                                obj.dataGrid.list.datagrid('updateRow', {index: index, row: {netStatus: '<i class="fa fa-circle fa-lg text-muted"></i>'}});
                            }
                        });
                    });
                }
            }
        },
    }
}